import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.*;
public class TextAreaTest {
    public static Highlighter highlighter;
    public static Highlighter subhighlighter;
    public static Highlighter.HighlightPainter painter = new DefaultHighlighter.DefaultHighlightPainter(Color.pink);
    public static Highlighter.HighlightPainter subpainter = new DefaultHighlighter.DefaultHighlightPainter(Color.pink);
    public static ArrayList<int[][]>coordinate = new ArrayList<int[][]>();
    public static int serchIndex = -1;
    public static int subCounter(String str,String subStr) {
        int count = 0;
        for (int i = 0; i < str.length() - subStr.length() + 1; i++) {
            if (str.substring(i,i + subStr.length()).equals(subStr)) {
                count++;
            }
        }
        return count;
    }
    public static void main(String[] args) throws BadLocationException {
        JFrame frame = new JFrame();
        frame.setSize(500,500);
        JTextArea ta = new JTextArea();
        ta.setLineWrap(true);
        ta.setText("aabbcc sdfksdjgkj aa aa346i46i5aa");
        Container container = frame.getContentPane();
        container.setLayout(new BorderLayout());


        JPanel serchBar = new JPanel(new BorderLayout());
        JPanel txtBar = new JPanel(new BorderLayout());
        JTextField serchTxt = new JTextField();
        JLabel value = new JLabel("100个");
        txtBar.add(serchTxt,BorderLayout.CENTER);
        txtBar.add(value,BorderLayout.EAST);


        JPanel btnBar = new JPanel(new BorderLayout());

        JButton a = new JButton("a");
        JButton b = new JButton("b");
        JButton c = new JButton("c");
        btnBar.add(a,BorderLayout.WEST);
        btnBar.add(b,BorderLayout.CENTER);
        btnBar.add(c,BorderLayout.EAST);

        serchBar.add(txtBar,BorderLayout.CENTER);
        serchBar.add(btnBar,BorderLayout.EAST);

        JButton jButton = new JButton("搜索");
        serchBar.setVisible(false);
        container.add(serchBar,BorderLayout.NORTH);
        container.add(new JScrollPane(ta),BorderLayout.CENTER);
        container.add(jButton,BorderLayout.SOUTH);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        c.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                highlighter.removeAllHighlights();
                subhighlighter.removeAllHighlights();
            }
        });


        jButton.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                serchBar.setVisible(true);
                highlighter = ta.getHighlighter();
                subhighlighter = ta.getHighlighter();
            }
        });

        b.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                highlighter.removeAllHighlights();
                serchIndex--;
                if (serchIndex < 0) {
                    serchIndex = coordinate.size() - 1;
                }
                int p0 = coordinate.get(serchIndex)[0][0];
                int p1 = coordinate.get(serchIndex)[0][1];
                try {
                    subhighlighter.addHighlight(p0,p1,subpainter);

                } catch (BadLocationException ex) {
                    ex.printStackTrace();
                }
            }
        });


        a.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                highlighter.removeAllHighlights();
                serchIndex++;
                if (serchIndex >= coordinate.size()) {
                    serchIndex = 0;
                }
                int p0 = coordinate.get(serchIndex)[0][0];
                int p1 = coordinate.get(serchIndex)[0][1];
                try {
                    highlighter.addHighlight(p0,p1,painter);
                } catch (BadLocationException ex) {
                    ex.printStackTrace();
                }
            }
        });

        serchTxt.getDocument().addDocumentListener(new DocumentListener() {
            @Override
            public void insertUpdate(DocumentEvent e) {
                coordinate.clear();
                String subStr = serchTxt.getText();
                int count = 0;
                for (int i = 0; i < ta.getText().length() - subStr.length() + 1; i++) {
                    if (ta.getText().substring(i,i + subStr.length()).equals(subStr)) {
                        count++;
                        value.setText(count + "个");
                        int p0 = i;
                        int p1 = p0 + subStr.length();
                        int[][] ints = {{p0,p1}};
                        coordinate.add(ints);
                        try {
                            highlighter.addHighlight(p0,p1,painter);
                        } catch (BadLocationException ex) {
                            ex.printStackTrace();
                        }
                    }
                }
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                coordinate.clear();
                if (serchTxt.getText().equals("")) {
                    highlighter.removeAllHighlights();
                    return;
                }
                highlighter.removeAllHighlights();
                String subStr = serchTxt.getText();
                int count = 0;
                for (int i = 0; i < ta.getText().length() - subStr.length() + 1; i++) {
                    if (ta.getText().substring(i,i + subStr.length()).equals(subStr)) {
                        count++;
                        value.setText(count + "个");
                        int p0 = i;
                        int p1 = p0 + subStr.length();
                        int[][] ints = {{p0,p1}};
                        coordinate.add(ints);
                        try {
                            highlighter.addHighlight(p0,p1,painter);
                        } catch (BadLocationException ex) {
                            ex.printStackTrace();
                        }
                    }
                }
            }
            @Override
            public void changedUpdate(DocumentEvent e) {

            }
        });


    }

}
